package com.jsgt.greekfire.ui.trading.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.TickMarket
import com.jsgt.greekfire.bean.TickMarketItem

class DepthAdapter : BaseAdapter<Any>() {
    var tickMarket: TickMarket? = null
    private var alBuyAccount = 0.0
    private var alSellAccount = 0.0
    private var buyAccount = 0.0
    private var sellAccount = 0.0
    private var countWidth = 0
    private val buyItemsData by lazy { ArrayList<TickMarketItem>() }
    private val sellItemsData by lazy { ArrayList<TickMarketItem>() }

    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_depth, parent, false)
        return VH(view)
    }

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var tvBuy: TextView? = itemView.findViewById(R.id.tvBuy)
        var tvBuyNumber: TextView? = itemView.findViewById(R.id.tvBuyNumber)
        var tvBuyPrice: TextView? = itemView.findViewById(R.id.tvBuyPrice)
        var tvSell: TextView? = itemView.findViewById(R.id.tvSell)
        var tvSellNumber: TextView? = itemView.findViewById(R.id.tvSellNumber)
        var tvSellPrice: TextView? = itemView.findViewById(R.id.tvSellPrice)
        var sellView: View? = itemView.findViewById(R.id.sellView)
        var buyView: View? = itemView.findViewById(R.id.buyView)

        fun bindData(position: Int, buy: TickMarketItem?, sell: TickMarketItem?) {
            tvBuy?.text = (position + 1).toString()
            tvSell?.text = (position + 1).toString()
            if (buy != null) {
                tvBuyNumber?.text = buy.residueNum.toString()
                tvBuyPrice?.text = buy.dealAmount.toString()

//                buyAccount = 0.0
//                for (i in 0..position) {
//                    buyAccount += tickMarket?.buy!![i].residueNum
//                }

                buyAccount = buy.residueNum
                val buyScale = buyAccount / alBuyAccount
                val buyParams = buyView?.layoutParams
                buyParams?.width = (countWidth * buyScale).toInt()
                //WonderfulLogUtils.logi("tag", " buyParams.width ==" + buyParams.width);
                //WonderfulLogUtils.logi("tag", " buyParams.width ==" + buyParams.width);
                buyView?.layoutParams = buyParams
            } else {
                tvBuyNumber?.text = "--"
                tvBuyPrice?.text = "--"
                val buyParams = buyView?.layoutParams
                buyParams?.width = 0
                buyView?.layoutParams = buyParams
            }
            if (sell != null) {
                tvSellNumber?.text = sell.residueNum.toString()
                tvSellPrice?.text = sell.dealAmount.toString()

//                sellAccount = 0.0
//                for (i in 0..position) {
//                    sellAccount += tickMarket?.sell!![i].residueNum
//                }

                sellAccount = sell.residueNum
                val sellScale = sellAccount / alSellAccount
                val sellParams = sellView?.layoutParams
                sellParams?.width = (countWidth * sellScale).toInt()
                //WonderfulLogUtils.logi("tag", " buyParams.width ==" + buyParams.width);
                //WonderfulLogUtils.logi("tag", " buyParams.width ==" + buyParams.width);
                sellView?.layoutParams = sellParams
            } else {
                tvSellNumber?.text = "--"
                tvSellPrice?.text = "--"
                val sellParams = sellView?.layoutParams
                sellParams?.width = 0
                sellView?.layoutParams = sellParams
            }
        }
    }

    override fun getItemCount(): Int {
        return 20
    }

    fun setWidth(countWidth: Int) {
        this.countWidth = countWidth
    }

    fun setData(tickMarket: TickMarket) {
        this.tickMarket = tickMarket
        buyItemsData.clear()
        sellItemsData.clear()

        alBuyAccount = 0.0
        tickMarket.buy?.forEach {
            alBuyAccount += it.residueNum
            buyItemsData.add(it)
        }

        alSellAccount = 0.0
        tickMarket.sell?.forEach {
            alSellAccount += it.residueNum
            sellItemsData.add(it)
        }
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as DepthAdapter.VH
        var buyData: TickMarketItem? = null
        var sellData: TickMarketItem? = null

        if (position < buyItemsData.size) {
            buyData = buyItemsData[position]
        }
        if (position < sellItemsData.size) {
            sellData = sellItemsData[position]
        }
        viewHolder.bindData(position, buyData, sellData)
    }
}